package com.dcits.helper;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

/**
 * 获取Spring容器的工具类
 */
public class SpringContextHolderHelper implements ApplicationContextAware {
    private static ApplicationContext context;

    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        SpringContextHolderHelper.context = context;
    }

    /**
     * 获取Spring容器中的Bean
     */
    public static <T> T getBean(Class<T> clazz) {
        if (context == null) {
            throw new IllegalStateException("ApplicationContext未初始化！");
        }
        return context.getBean(clazz);
    }

    /**
     * 获取配置项（从application.yml）
     */
    public static String getProperty(String key) {
        return context.getEnvironment().getProperty(key);
    }
}